package com.miplus.ccrm.core.service.crd;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.core.controller.vo.credit.CaseTagReqVo;
import com.miplus.ccrm.core.mapper.SysTagsMapper;
import com.miplus.ccrm.core.pojo.FiCreditcasesTag;
import com.miplus.ccrm.core.pojo.SysTags;
import com.miplus.ccrm.core.service.crd.FiCreditcasesTagService;
import com.miplus.ccrm.core.mapper.FiCreditcasesTagMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author Yaohui
* @description 针对表【fi_creditcases_tag(案件标签关联表)】的数据库操作Service实现
* @createDate 2024-11-01 11:20:04
*/
@Service
public class FiCreditcasesTagServiceImpl extends MPJBaseServiceImpl<FiCreditcasesTagMapper, FiCreditcasesTag>
    implements FiCreditcasesTagService{

    @Resource
    private SysTagsMapper sysTagsMapper;
    @Override
    public CmdResVo caseTag(CaseTagReqVo reqVo) {


        if(reqVo.getAction().equals("2")){
            // 下标  删除指定标签
            remove(new QueryWrapper<FiCreditcasesTag>()
                    .in("creditcaseid",reqVo.getCaseId())
                    .ne("tid","FS")
                    .in("id",reqVo.getTags())
            );//删除所有
        }


        if(reqVo.getAction().equals("1")){
            if("1".equals(reqVo.getForceDelete())){
                remove(new QueryWrapper<FiCreditcasesTag>()
                        .ne("tid","FS")
                        .in("creditcaseid",reqVo.getCaseId())
                );
            }


            if("2".equals(reqVo.getForceDelete())){
                remove(new QueryWrapper<FiCreditcasesTag>()
                        .in("creditcaseid",reqVo.getCaseId())
                        .ne("tid","FS")
                        .in("id",reqVo.getTags())
                );//删除所有标签
            }

            //打标
            if(reqVo.getTags()!=null && reqVo.getTags().size()>0){

                //开始进行打标
                for (Integer caseId : reqVo.getCaseId()) {
                    List<FiCreditcasesTag> tagList=new ArrayList<>();

                    for (String tag : reqVo.getTags()) {
                        if(StringUtils.isEmpty(tag))
                            continue;
                        SysTags sysTags=sysTagsMapper.tagsSelect(tag);
                        FiCreditcasesTag fiCreditcasesTag=new FiCreditcasesTag();
                        fiCreditcasesTag.setId(tag);
                        fiCreditcasesTag.setTid(sysTags.getTid());
                        fiCreditcasesTag.setCreditcaseid(Long.valueOf(caseId));
                        tagList.add(fiCreditcasesTag);
                    }

                    saveBatch(tagList);
                }

            }
        }


        return new CmdResVo(reqVo.getCaseId().size(),"ok");
    }
}




